﻿using System;
using System.Windows.Forms;
using CodePlex.DnD4eCampaignManager.ControlLibrary.ControlExtensions;
using CodePlex.DnD4eCampaignManager.DnD4e.Enums;
using CodePlex.DnD4eCampaignManager.Presentation;
using CodePlex.DnD4eCampaignManager.Presentation.ViewInterfaces;
using CodePlex.DnD4eCampaignManager.Repositories.Interfaces;

namespace CodePlex.DnD4eCampaignManager.CampaignManagerControlLibrary
{
    public partial class PlayerCharacterControl : UserControl, IPlayerCharacterView
    {
        public PlayerCharacterControl(string playerCharacterName, IPlayerCharacterRepository playerCharacterRepository)
        {
            InitializeComponent();
            GenderList.Items.Clear();
            GenderList.Items.Add(Gender.Male);
            GenderList.Items.Add(Gender.Female);
            GenderList.SelectedItem = Gender.Male;
            AlignmentList.Items.Clear();
            AlignmentList.Items.Add(Alignment.LawfulGood);
            AlignmentList.Items.Add(Alignment.Good);
            AlignmentList.Items.Add(Alignment.Unaligned);
            AlignmentList.Items.Add(Alignment.Evil);
            AlignmentList.Items.Add(Alignment.ChaoticEvil);
            AlignmentList.SelectedItem = Alignment.Unaligned;
            SizeList.Items.Clear();
            SizeList.Items.Add(CreatureSize.Tiny);
            SizeList.Items.Add(CreatureSize.Small);
            SizeList.Items.Add(CreatureSize.Medium);
            SizeList.Items.Add(CreatureSize.Large);
            SizeList.Items.Add(CreatureSize.Huge);
            SizeList.Items.Add(CreatureSize.Gargantuan);
            SizeList.SelectedItem = CreatureSize.Medium;
            Presenter = new PlayerCharacterPresenter(playerCharacterName, this, playerCharacterRepository);
            Presenter.DisplayPlayerCharacter();
        }

        public int AbilityCharisma
        {
            get { return CharismaScoreNumeric.IntegerValue(); }
            set { CharismaScoreNumeric.Value = value; }
        }

        public int AbilityConstitution
        {
            get { return ConstitutionScoreNumeric.IntegerValue(); }
            set { ConstitutionScoreNumeric.Value = value; }
        }

        public int AbilityDexterity
        {
            get { return DexterityScoreNumeric.IntegerValue(); }
            set { DexterityScoreNumeric.Value = value; }
        }

        public int AbilityIntelligence
        {
            get { return IntelligenceScoreNumeric.IntegerValue(); }
            set { IntelligenceScoreNumeric.Value = value; }
        }

        public int AbilityStrength
        {
            get { return StrengthScoreNumeric.IntegerValue(); }
            set { StrengthScoreNumeric.Value = value; }
        }

        public int AbilityWisdom
        {
            get { return WisdomScoreNumeric.IntegerValue(); }
            set { WisdomScoreNumeric.Value = value; }
        }

        public int ActionPoints
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public bool IsBloodied
        {
            get { throw new NotImplementedException(); }
        }

        public int CharacterAge
        {
            get { return AgeNumeric.IntegerValue(); }
            set { SetNumericValue(value, AgeNumeric); }
        }

        public Alignment CharacterAlignment
        {
            get { return (Alignment) AlignmentList.SelectedItem; }
            set { AlignmentList.SelectedItem = value; }
        }

        public string CharacterClass
        {
            get { return ClassText.Text; }
            set { ClassText.Text = value; }
        }

        public string CharacterDeity
        {
            get { return DeityText.Text; }
            set { DeityText.Text = value; }
        }

        public string CharacterEpicDestiny
        {
            get { return EpicDestinyText.Text; }
            set { EpicDestinyText.Text = value; }
        }

        public int CharacterExperiencePoints
        {
            get { return ExperiencePointsNumeric.IntegerValue(); }

            set { SetNumericValue(value, ExperiencePointsNumeric); }
        }

        public string CharacterFeats
        {
            get { return FeatsText.Text; }
            set { FeatsText.Text = value; }
        }

        public Gender CharacterGender
        {
            get { return (Gender) GenderList.SelectedItem; }
            set { GenderList.SelectedItem = value; }
        }

        public string CharacterHeight
        {
            get { return HeightText.Text; }
            set { HeightText.Text = value; }
        }

        public int CharacterLevel
        {
            set { LevelText.Text = value.ToString(); }
            get { return Convert.ToInt32(LevelText.Text); }
        }

        public string CharacterName
        {
            set { NameText.Text = value; }
        }

        public string CharacterParagonPath
        {
            get { return ParagonText.Text; }
            set { ParagonText.Text = value; }
        }

        public string CharacterRace
        {
            get { return RaceText.Text; }
            set { RaceText.Text = value; }
        }

        public string CharacterRituals
        {
            get { return RitualsText.Text; }
            set { RitualsText.Text = value; }
        }

        public int CharacterWeight
        {
            get { return WeightNumeric.IntegerValue(); }
            set { SetNumericValue(value, WeightNumeric); }
        }

        public string Conditions
        {
            get { return ConditionsText.Text; }
            set { ConditionsText.Text = value; }
        }

        public CreatureSize CreatureSize
        {
            get { return (CreatureSize) SizeList.SelectedItem; }
            set { SizeList.SelectedItem = value; }
        }

        public int DefenseArmorClass { get; set; }

        public int DefenseFortitude
        {
            get { return FortitudeDefenseNumeric.IntegerValue(); }
            set { FortitudeDefenseNumeric.Value = value; }
        }

        public int DefenseReflex
        {
            get { return ReflexDefenseNumeric.IntegerValue(); }
            set { ReflexDefenseNumeric.Value = value; }
        }

        public int DefenseWillpower
        {
            get { return WillpowerDefenseNumeric.IntegerValue(); }
            set { WillpowerDefenseNumeric.Value = value; }
        }

        public string DisplayName
        {
            get { throw new NotImplementedException(); }
        }

        public int HealingSurgesCurrent
        {
            get { return CurrentHealingSurgesNumeric.IntegerValue(); }
            set { SetNumericValue(value, CurrentHealingSurgesNumeric); }
        }

        public int HealingSurgesMaximum { get; set; }

        public int HealingSurgeValue
        {
            get { return HealingSurgeValueNumeric.IntegerValue(); }
            set { HealingSurgeValueNumeric.Value = value; }
        }

        public int HitPointsBloodied
        {
            get { throw new NotImplementedException(); }
        }

        public int HitPointsCurrent
        {
            get { return CurrentHitPointsNumeric.IntegerValue(); }
            set { SetNumericValue(value, CurrentHitPointsNumeric); }
        }

        public int HitPointsMaximum
        {
            get { return HitPointsNumeric.IntegerValue(); }
            set { HitPointsNumeric.Value = value; }
        }

        public int HitPointsTemporary
        {
            get { return TemporaryHitPointsNumeric.IntegerValue(); }
            set { TemporaryHitPointsNumeric.Value = value; }
        }

        public int? Index
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
        public int FailedDeathSaves
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public int InitiativeModifier
        {
            get { return InitiativeModifierNumeric.IntegerValue(); }
            set { InitiativeModifierNumeric.Value = value; }
        }

        public int InitiativeRoll
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public int InitiativeScore
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public int MovementSpeed
        {
            get { return MovementSpeedNumeric.IntegerValue(); }
            set { SetNumericValue(value, MovementSpeedNumeric); }
        }

        public string MovementSpeedOther
        {
            get { return SpeedAdditionalText.Text; }
            set { SpeedAdditionalText.Text = value; }
        }

        public int MovementStealth
        {
            get { return StealthNumeric.IntegerValue(); }
            set { SetNumericValue(value, StealthNumeric); }
        }

        public string PowersAtWill
        {
            get { return AtWillPowersText.Text; }
            set { AtWillPowersText.Text = value; }
        }

        public string PowersDaily
        {
            get { return DailyPowersText.Text; }
            set { DailyPowersText.Text = value; }
        }

        public string PowersEncounter
        {
            get { return EncounterPowersText.Text; }
            set { EncounterPowersText.Text = value; }
        }

        public int SensesInsight
        {
            get { return InsightNumeric.IntegerValue(); }
            set { InsightNumeric.Value = value; }
        }

        public int SensesInsightPassive
        {
            get { return InsightPassiveNumeric.IntegerValue(); }
            set { InsightPassiveNumeric.Value = value; }
        }

        public string SensesOther
        {
            get { return SensesSpecialText.Text; }
            set { SensesSpecialText.Text = value; }
        }

        public int SensesPerception
        {
            get { return PerceptionNumeric.IntegerValue(); }
            set { PerceptionNumeric.Value = value; }
        }

        public int SensesPerceptionPassive
        {
            get { return PerceptionPassiveNumeric.IntegerValue(); }
            set { PerceptionPassiveNumeric.Value = value; }
        }

        public string Special
        {
            get { return SensesSpecialText.Text; }
            set { SensesSpecialText.Text = value; }
        }

        public string SpecialFeatures
        {
            get { return SpecialFeaturesText.Text; }
            set { SpecialFeaturesText.Text = value; }
        }

        public string SpecialQualities
        {
            get { return SpecialFeaturesText.Text; }
            set { SpecialFeaturesText.Text = value; }
        }

        public string Tactics
        {
            get { return TacticsText.Text; }
            set { TacticsText.Text = value; }
        }


        private PlayerCharacterPresenter Presenter { get; set; }


        public void Damage(int hitPointsLost)
        {
            throw new NotImplementedException();
        }

        public string FormatCombatantForDisplay()
        {
            throw new NotImplementedException();
        }

        public void Heal(int hitPointsRecovered)
        {
            throw new NotImplementedException();
        }

        public void SpendHealingSurge()
        {
            throw new NotImplementedException();
        }


        private void CurrentHitPointsNumeric_ValueChanged(object sender, EventArgs e)
        {
        }

        private void PlayerCharacterControl_Load(object sender, EventArgs e)
        {
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            Presenter.SavePlayerCharacter();
            Presenter.DisplayPlayerCharacter();
        }

        private static void SetNumericValue(int value, NumericUpDown numericUpDown)
        {
            if ( value < numericUpDown.Minimum )
            {
                numericUpDown.Value = numericUpDown.Minimum;
            }
            else if ( value > numericUpDown.Maximum )
            {
                numericUpDown.Value = numericUpDown.Maximum;
            }
            else
            {
                numericUpDown.Value = value;
            }
        }
    }
}